<!DOCTYPE html>
<html>
	<head>
	    <meta charset="utf-8">
	    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width" />
	    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
	    <title>Hello APP</title>
	    <link rel="stylesheet" type="text/css" href="../../../css/index.css" />
	</head>
	<style type="text/css">
		.warp{width: 100%;}

		.time{width: 100%;height: 44px;background:#FFF1DD;color: #FF9502;line-height: 44px;padding: 0 15px;box-sizing: border-box;font-size: 15px;}
		.info-one{height: 44px;background: #FFFFFF;align-items:center;justify-content: space-between;padding: 0 15px;box-sizing: border-box;margin-bottom: 10px;}
		.info-one .l{font-size: 15px;}
		.info-one .r{font-size: 16px;color: #FF9502;}
		.title{height: 50px;padding: 0 15px;line-height: 50px;color: #8E9194;font-size: 13px;background: #FFFFFF;}
		.sel-pay{width: 100%;}
		.sel-pay .sel-one{padding: 0 15px;box-sizing: border-box;align-items:center;height: 50px;background: url(../../../image/main2/sel_icon0.png) no-repeat right 15px center #FFFFFF;background-size:18px;}
		.sel-pay .sel-one.on{background: url(../../../image/main2/sel_icon1.png) no-repeat right 15px center #FFFFFF;background-size:18px;}
		.sel-pay .sel-one img{width: 30px;height: 30px;}
		.sel-pay .sel-one .text{padding-left: 8px;box-sizing: border-box;}
		.zhifu-btn{margin:50px 15px;background:#FF9502;border-radius:4px;text-align: center;line-height: 45px;font-size: 16px;color: #FFFFFF;}
	</style>
	<body>

		<div id="vue" class="warp">

			<!--剩余时间-->
			<div class="time" style="display:none;" :style="{ display:(endtype == 1 ? '':'none')}" id="timeBox">请在<i id="hour"></i>小时<i id="min"></i>分<i id="sec"></i>秒内完成支付</div>
			<!--支付金额-->
			<div class="info-one flex">
				<div class="l">需支付金额</div>
				<div class="r" >¥<i v-text="price"></i></div>
			</div>
			<!--选择支付方式-->
			<div class="title border-bottom">支付方式</div>
			<div class="sel-pay">
				<div class="sel-one flex border-bottom on" onclick="tab_type(this);" data-type="wxpay">
					<img src="../../../image/main2/pay_sel2.png"/>
					<div class="text">微信支付</div>
				</div>
				<div class="sel-one flex" onclick="tab_type(this);" data-type="alipay">
					<img src="../../../image/main2/pay_sel3.png"/>
					<div class="text">支付宝支付</div>
				</div>
			</div>
			<div class="zhifu-btn" tapmode onclick="goPay()">确定</div>
		</div>
	</body>
</html>
<script type="text/javascript" src="../../../script/base.min.js" id="loadjs" data-js="L/swiper.min,L/lazyload,L/h-tool"></script>
<script type="text/javascript">
	var payData = {};
	define(function (require, exports, module) {
		pageParam = api.pageParam;
		payData.orderid = pageParam.id;
		payData.paytype = 'wxpay';
		payData.price = pageParam.price;

		//console.log(JSON.stringify(pageParam));

		//关掉确认订单页面
		if(pageParam && pageParam.from){
			if(pageParam.from == "ordert"){
				setTimeout(function (){
					api.execScript({name:"ordert_win",frameName:'ordert', script:'closewin();'});
				}, 300)
			}
		}

		lanYun.newVue('#vue',{
			price:pageParam.price,
			endmiao:pageParam.endmiao,
			endtype:pageParam.endtype
	    },function() {

	    });

		//去支付
		goPay = function (){
			//console.log(JSON.stringify(payData));
			var url = 'OrderPay/pay';
			if(pageParam.action == 'ruzhu'){
				url = 'UserStoreIn/pay';
			}
			lanYun.post( url , payData ,function (res) {
				//console.log(JSON.stringify(res));
				if(res.errcode == 0){
					if(res.paysta == "err"){
						lanYun.toast(res.msg);
						return;
					}
					if(res.paysta == "ok"){
						lanYun.toast(res.msg);
						paysta_result(1);
						return;
					}
					if(res.paytype == 'alipay'){
						var aliPayPlus = api.require('aliPayPlus');
						aliPayPlus.payOrder({
							orderInfo:res.paystr
						}, function(ret, err) {
							if(ret.code == '9000'){
								lanYun.toast('支付成功');
								paysta_result(1);
							}else if(ret.code == '6001'){
								lanYun.toast('用户取消支付');
								paysta_result(0);
							}else{
								lanYun.toast('支付失败');
								paysta_result(0);
							}
						});
						return ;
					}
					if(res.paytype == 'wxpay'){
						var wxPayPlus = api.require('wxPayPlus');
						var payret = res.paydata;
						wxPayPlus.payOrder({
							apiKey:payret.appid,
							orderId: payret.prepayid,
							mchId: payret.partnerid,
							nonceStr: payret.noncestr,
							timeStamp: payret.timestamp,
							package:payret.package,
							sign: payret.sign
						}, function(ret, err) {
							if (ret.status) {
								lanYun.toast('支付成功');
								paysta_result(1);
							} else {
								if(err.code == -2){
									lanYun.toast('取消支付');
								}else{
									lanYun.toast('未知错误');
								}
								paysta_result(0);
							}
						});
					}
				}else if(res.errcode == 2){
					lanYun.alert({title:'提示',msg:res.msg},function(){
						api.closeWin();
					});
				}else{
					lanYun.toast(res.msg);
				}
	        },2);
		}

		//去支付状态页面
		paysta_result = function (sta) {
			var tit = sta ? '支付成功' : '支付失败';
			payData.sta = sta;
			payData.ani = 1;
			payData.type = pageParam.action;
			if(pageParam.action == 'repay' && sta == 1){
				api.execScript({name:"ordert/ordert_list.html_win",frameName:'ordert/ordert_list.html', script:'intGetData();'});
			}
			//console.log(JSON.stringify(payData));
			lanYun.openFrame('goods/pay/paymsg.html',payData,function (data) {
				//console.log(JSON.stringify(data));
				lanYun.vue.price = data.orderPayPrice;
				lanYun.vue.endtype = data.endtype;
				if(data.endtype == 1){
					countDown(data.endmiao);
				}
			});
			lanYun.event('uporderNum');
		}

		//切换头部类型
		tab_type = function (tis) {
	        var dst = tis.dataset;
			$(tis).addClass('on').siblings().removeClass('on');
        	payData.paytype = dst.type;
	    }

		// 写入倒计时
		if(pageParam.endtype == 1){
			countDown(pageParam.endmiao);
		}

		function countDown(times) {
			var fun = function(){
				var day = 0,
					hour = 0,
					minute = 0,
					second = 0;//时间默认值
				if (times > 0) {
					day = Math.floor(times / (24 * 60 * 60));
					hour = Math.floor(times / (60 * 60)) - (day * 24);
					minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);
					second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
				}
				if (day <= 9) day = '0' + day;
				if (hour <= 9) hour = '0' + hour;
				if (minute <= 9) minute = '0' + minute;
				if (second <= 9) second = '0' + second;
				$('#hour').html(hour);$('#min').html(minute);$('#sec').html(second);
				times--;
				if (times <= 0) {
					clearInterval(timer);
					$('#timeBox').html('支付超时');
					lanYun.alert({fname:'diaLogBox1',title:'提示', button:['确定'],content: "支付已超时" },function(ret){
                        api.closeWin();
        	        });
				}
			};
			fun();
			var timer = setInterval(function () {
				fun();
			}, 1000);
		}

		//页面关闭方法
		backMenu = function (tis){
			api.closeWin();
		}

	});

</script>
